#include "tcp_dispatcher.h"

using namespace VLRTSP;


/**
 * @Author: CimZzz
 * @Date: 2022-01-17 00:01:29
 * 
 * 构造函数
 * @param writeQueue 写队列
 * @param id 媒体分发器 Id
 * @param rtpSendPoint RTP 发送端口 / Channel
 * @param rtcpSendPoint RTCP 发送端口 / Channel
 * @param mediaSource 媒体资源
 */
TCPDispatcher::TCPDispatcher(
	QueueThread<Buffer>* writeQueue,
	uint32_t id,
	uint32_t rtpSendPoint,
	uint32_t rtcpSendPoint,
	std::shared_ptr<MediaSource> mediaSource
): MediaDispatcher(id, rtpSendPoint, rtcpSendPoint, mediaSource) {
	this->writeQueue = writeQueue;
}

/**
 * @Author: CimZzz
 * @Date: 2022-01-17 00:01:41
 * 
 * 析构函数
 */
TCPDispatcher::~TCPDispatcher() {

}

/**
 * @Author: CimZzz
 * @Date: 2022-01-17 00:02:24
 * 
 * 消息队列回调函数
 * @param sendBuffer 待发送的 RTP 帧
 */
void TCPDispatcher::onRecvSendBuffer(std::shared_ptr<RTPFrame>& sendBuffer) {
	Buffer* buffer = new Buffer(4);
	uint16_t bufferSize = sendBuffer.get()->buffer.get()->getBufferLength();
	buffer->appendByte('$');
	buffer->appendByte((uint8_t)this->getRTPStartPoint());
	buffer->appendByte((bufferSize & 0xFF00) >> 8);
	buffer->appendByte(bufferSize & 0xFF);
	this->writeQueue->send(buffer);
	this->writeQueue->send(sendBuffer.get()->buffer);
}